class BlueToothUtils
  constructor: (options)->
    @isOpen = false;
    @_discoveryStarted = false;
    @connected = false;
    @options =
      device: "unknow"
      deviceId: "unknow"
      deviceName: "unknow"
      deviceAddress: "unknow"
      deviceType: "unknow"
      characteristicUUID: "unknow"
      serviceUUID: "unknow"
      loadingTips: (res = {type: 1, msg: "加载中"})->
        console.log("loadingTips", type, msg)
    @initBlues(options);
  initBlues: (options)->
    @options = options
    @
  startScan: ()->
    that = @
    if @isOpen
      that.getBluetoothAdapterState()
    else
      that.openBluetoothAdapter()
    @

  startBluetoothDevicesDiscovery: ()->
    that = @
    return if @_discoveryStarted
    @_discoveryStarted = true
    #    app.showLoading '正在搜索设备...'
    @options.loadingTips({type: 1, msg: '正在搜索设备...'})
    setTimeout ()->
      that.stopBluetoothDevicesDiscovery()
    , 3000
    wx.startBluetoothDevicesDiscovery
      services: [that.options.serviceUUID]
      powerLevel: 'high',
      allowDuplicatesKey: false,
      success: ()->
        that.onBluetoothDeviceFound()
#        that.getBluetoothDevices()
    @
  openBluetoothAdapter: ()->
    that = @
    wx.openBluetoothAdapter
      success: (res)->
        that.isOpen = true
        that.getBluetoothAdapterState()
      fail: (res)->
        that.isOpen = false
        that.options.loadingTips({type: 2, msg: '蓝牙适配器不可用'})
    #          reject({msg: "蓝牙适配器不可用"})
    wx.onBluetoothAdapterStateChange (res)->
      if res.available
        that.getBluetoothAdapterState()
      else
        that.stopBluetoothDevicesDiscovery()
        that.devices = [];
        that.options.loadingTips({type: 2, msg: '蓝牙开关未打开'})
    @
  closeBluetoothAdapter: ()->
    wx.closeBluetoothAdapter()
    @_discoveryStarted = false
    @
  getBluetoothAdapterState: ()->
    that = @
    wx.getBluetoothAdapterState
      success: (res) ->
        isDiscov = res.discovering #是否正在搜索设备
        isDvailable = res.available #蓝牙适配器是否可用
        that.startBluetoothDevicesDiscovery() if isDvailable and !isDiscov
    @
  onBluetoothDeviceFound: ()->
    that = @
    wx.onBluetoothDeviceFound (res)->
      for device in res.devices
        if device.name == that.options.deviceName
          that.createBLEConnection(device.deviceId)
          break
    @
  createBLEConnection: (e) ->
    that = @
    ds = e.currentTarget.dataset
    devId = ds.deviceId #设备UUID
    name = ds.name #设备名
    that.stopConnectDevices() #配对之前先断开已连接设备
    wx.createBLEConnection
      deviceId: devId
      success: (res)->
        that.connected = true
        that.options.device_id = devId
        that.name = name
        that.getBLEDeviceServices(devId)
      fail: (res)->
        that.data.connected = false
        if res.errCode == 10002
          that.options.loadingTips({type: 2, msg: '链接超时，请重试'})
#          app.showError '链接超时，请重试'
        else if res.errCode == 10003
          that.options.loadingTips({type: 2, msg: '链接蓝牙失败，蓝牙地址无效'})
#          app.showError '链接蓝牙失败，蓝牙地址无效'
        else
          that.options.loadingTips({type: 2, msg: '链接超时，请重试'})
        #          app.showError '链接蓝牙失败，请重试'
        that.closeBLEConnection()
    that.stopBluetoothDevicesDiscovery() #停止搜索
    @
  closeBLEConnection: ()->
    that = @
    wx.closeBLEConnection
      deviceId: that.data.deviceId
    @
module.exports =
  BlueToothUtils: BlueToothUtils
